/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package PropertiesController;

import Util.StringUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import nl.chess.it.util.config.Config;
import nl.chess.it.util.config.ConfigurationException;

/**
 *
 * @author Peat
 */
public class BaseModuleProperties extends Config {

    private List<String> viewNameArr = new ArrayList<String>();
    private List<String> requiredOTPView = new ArrayList<String>();
    private List<String> requiredSessionView = new ArrayList<String>();
    private String defaultPage;
    private String modulePath;

    public BaseModuleProperties(String resourceName) throws ConfigurationException {
        super(resourceName);
        defaultPage = getString("property.ViewPageNoParam");
        modulePath = getString("property.ModulePath");
        viewNameArr = Arrays.asList(getString("property.ViewList").split("\\|"));
        requiredOTPView = Arrays.asList(getString("property.requiredOTPView").split("\\|"));
        requiredSessionView = Arrays.asList(getString("property.requiredSessionView").split("\\|"));
    }

    public boolean isValidPage(String pageName) {
        return viewNameArr.contains(pageName);
    }

    public boolean isRequiredOTP(String pageName) {
        return requiredOTPView.contains(pageName);
    }

    public boolean isRequiredValidSession(String pageName) {
        return requiredSessionView.contains(pageName);
    }

    public String getModulePath() {
        return modulePath;
    }

    public String getViewPath(HttpServletRequest request){
        String page = null;
        try{
            page = request.getParameter("page");
            if(StringUtils.isNullOrEmpty(page)){
                page = defaultPage;
                System.out.println("\t-Landing on [PAGE PARAM MISSING]: " + page);
            }else{
                if(isValidPage(page)){
                    System.out.println("\t-Landing on [PAGE FOUND]: " + page);
                }else{
                    page = null;
                    System.out.println("\t-Landing on [PAGE NOT FOUND]: " + page);
                }
            }

        }catch(Exception ex){
            page = defaultPage;
            System.out.println("\t-Landing on [EX]: " + page + "\t EX=" + ex + "\r\n\t req=" + request);
        }
        return page;
    }
}
